import 'package:dio/dio.dart';
import 'package:qunaimedical/network/interceptors.dart';
import 'package:qunaimedical/utils/log_utils.dart';
import 'package:qunaimedical/values/urls.dart';

class XHttp {
  XHttp._internal();

  static const String _tag = 'Http';

  /// 网络请求配置
  static final Dio _dio = Dio(BaseOptions(
    baseUrl: Urls.BASE_URL,
    connectTimeout: 15000,
    receiveTimeout: 5000,
  ));

  /// 初始化dio
  static init() {
    //添加拦截器
    _dio.interceptors.add(CustomInterceptors());
  }

  /// get请求
  static Future get(String url, {Map<String, dynamic> params}) async {
    try {
      Response response;
      if (params == null) {
        response = await _dio.get(url);
      } else {
        response = await _dio.get(url, queryParameters: params);
      }
      return response.data;
    } on DioError catch (e) {
      Log.d(_tag, e);
      if (e.response != null && e.response.data.runtimeType != String) {
        if (e.response.statusCode == null ||
            e.response.statusCode.toString().startsWith('5')) {
          return null;
        }
        return e.response.data;
      } else {
        return null;
      }
    } catch (e) {
      Log.d(_tag, e);
      return null;
    }
  }

  /// delete请求
  static Future delete(String url) async {
    try {
      Response response;
      response = await _dio.delete(url);
      return response.data;
    } on DioError catch (e) {
      Log.d(_tag, e);
      if (e.response != null && e.response.data.runtimeType != String) {
        if (e.response.statusCode == null ||
            e.response.statusCode.toString().startsWith('5')) {
          return null;
        }
        return e.response.data;
      } else {
        return null;
      }
    } catch (e) {
      Log.d(_tag, e);
      return null;
    }
  }

  /// post body请求
  static Future postJson(String url, [Map<String, dynamic> data]) async {
    try {
      Response response = await _dio.post(url, data: data);
      return response.data;
    } on DioError catch (e) {
      Log.d(_tag, e);
      if (e.response != null && e.response.data.runtimeType != String) {
        if (e.response.statusCode == null ||
            e.response.statusCode.toString().startsWith('5')) {
          return null;
        }
        return e.response.data;
      } else {
        return null;
      }
    } catch (e) {
      Log.d(_tag, e);
      return null;
    }
  }

  /// post form-data
  static Future postFormData(String url, [Map<String, dynamic> data]) async {
    try {
      Response response;
      if (data == null) {
        response = await _dio.post(url);
      } else {
        response = await _dio.post(url, data: FormData.fromMap(data));
      }
      return response.data;
    } on DioError catch (e) {
      Log.d(_tag, e);
      if (e.response != null && e.response.data.runtimeType != String) {
        if (e.response.statusCode == null ||
            e.response.statusCode.toString().startsWith('5')) {
          return null;
        }
        return e.response.data;
      } else {
        return null;
      }
    } catch (e) {
      Log.d(_tag, e);
      return null;
    }
  }

  /// upload files
  static Future uploadFiles(String url, FormData data) async {
    try {
      Response response = await _dio.post(url, data: data);
      return response.data;
    } on DioError catch (e) {
      Log.d(_tag, e);
      if (e.response != null && e.response.data.runtimeType != String) {
        if (e.response.statusCode == null ||
            e.response.statusCode.toString().startsWith('5')) {
          return null;
        }
        return e.response.data;
      } else {
        return null;
      }
    } catch (e) {
      Log.d(_tag, e);
      return null;
    }
  }
}
